package com.ccnf.web.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 生成API接口文档的swagger配置类
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", name = "enabled", havingValue = "true")
public class SwaggerConfig {
    @Value("${swagger.enabled}")
    private boolean enabledSwagger;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// 设置API访问时的content-path路径
                .apiInfo(apiInfo())
                .enable(enabledSwagger)
                .select()
                // 指定Swagger的扫描包,假如不指定此项，在Spring Boot项目中，会生成base-err-controller的api接口项
//                .apis(RequestHandlerSelectors.basePackage("com.ccnf.task.web.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//这里采用包含注解的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("分享邀约管家 API接口文档")
                .description("分享邀约管家 API接口文档描述")
                .contact(new Contact("会管家", "http://www.ccnf.com", "930168863@qq.com"))
                .termsOfServiceUrl("http://localhost:8085")
                .version("2.0")
                .build();
    }
}